package com.wl.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wl.dao.CommentDao;
import com.wl.enums.CommentType;
import com.wl.enums.Status;
import com.wl.model.Comment;
import com.wl.service.CommentService;
import com.wl.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.ibatis.session.RowBounds;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by wu on 2018/4/16.
 */
@Service("commentServiceImpl")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    /**
     * 评论列表
     */
    @Override
    public Pair<Integer, List<Comment>> commentList(Integer page, Integer pageSize, Long userId, String content) {
        List<Comment> list;
        Wrapper<Comment> wrapper = new EntityWrapper<Comment>();
        if (!content.isEmpty()) {
            wrapper.like("content", "%" + content + "%");
        }
        if (userId != null) {
            wrapper.eq("create_by", userId);
        }
        if (page != null && pageSize != null) {
            RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
            list = commentDao.selectPage(rowBounds, wrapper);
        } else {
            list = commentDao.selectList(wrapper);
        }
        Integer total = commentDao.selectCount(wrapper);
        Pair<Integer, List<Comment>> of = Pair.of(total, list);
        return of;
    }

    /**
     * 添加评论
     */
    @Transactional
    @Override
    public Comment add(String content, CommentType type, Long id, Long userId) {
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setStatus(Status.enabled.name());
        comment.setParentType(type.name());
        comment.setParentId(id);
        comment.setCreateBy(userId);
        comment.setCreateTime(new Date());
        commentDao.insert(comment);
        return null;
    }

    /**
     * 删除评论
     */
    @Transactional
    @Override
    public void del(Long commentId) {
        Comment comment = commentDao.selectById(commentId);
        comment.setStatus(Status.disabled.name());
        commentDao.updateById(comment);
    }

    @Override
    public List<Comment> getLimitComment(PageUtil pageUtil) {
        return commentDao.getLimitComment(pageUtil);
    }

    @Override
    public Integer getCountComment() {
        return commentDao.getCountComment();
    }

    @Override
    public void updateComment(Comment comment) {
        commentDao.updateById(comment);
    }

    @Override
    public void addComment(Comment comment) {
        comment.setCreateBy(1L);
        comment.setCreateTime(new Date());
        commentDao.insert(comment);
    }

    @Override
    public Comment getComment(long commentId) {
        return commentDao.selectById(commentId);
    }

    @Override
    public void deleteComment(long commentId) {
        commentDao.deleteById(commentId);
    }

    @Override
    public List<Comment> findByComment(Comment comment) {
        if (comment.getContent().trim().isEmpty()){
            comment.setContent(null);
        }
        if (comment.getParentType().trim().isEmpty()){
            comment.setParentType(null);
        }
        return commentDao.findByComment(comment);
    }

    @Override
    public List<Comment> findByPid(Long pid) {
        EntityWrapper<Comment> wrapper = new EntityWrapper<>();
        wrapper.eq("parent_id", pid);
        return commentDao.selectList(wrapper);
    }
}
